/*
 * @Author: alvin
 * @Date: 2021-02-23 18:43:57
 * @Last Modified by:  alvin
 * @Last Modified time: 2021-02-23 18:43:57
 */

const cp = require('child_process');

let error = false;

const majorNodeVersion = parseInt(/^(\d+)\./.exec(process.versions.node)[1]);

if (majorNodeVersion < 10) {
  console.error('\033[1;31m*** Please use node >=10.\033[0;0m');
  error = true;
}

const yarnVersion = cp.execSync('yarn -v', { encoding: 'utf8' }).trim();
const parsedYarnVersion = /^(\d+)\.(\d+)\./.exec(yarnVersion);
const majorYarnVersion = parseInt(parsedYarnVersion[1]);
const minorYarnVersion = parseInt(parsedYarnVersion[2]);

if (majorYarnVersion < 1 || minorYarnVersion < 10) {
  console.error('\033[1;31m*** Please use yarn >=1.10.1.\033[0;0m');
  error = true;
}

console.log(process.env['npm_execpath']);

if (!/yarn[\w-.]*\.js$|yarnpkg$/.test(process.env['npm_execpath'])) {
  console.error('\033[1;31m*** Please use yarn to install dependencies.\033[0;0m');
  error = true;
}

if (error) {
  console.error('');
  process.exit(1);
}
